package com.example.werewolf.entity;

import com.example.common.common.config.llm.RedisMemoryRepository;
import com.example.werewolf.common.Camp;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Player {
    private int id;
    private String role; // 角色
    private Camp camp; // 0 平民 1好人（常为神职） 2狼人（狼人或转化为狼人）
    private boolean isAlive; // 玩家是否存活
    private boolean isKilled; // 昨晚是否被刀
    private boolean isPoison; // 是否被毒
    private boolean isSpeech; // 玩家是否上警
    private boolean hasActed; // 玩家是否已经行动过
    private List<String> actions; // 玩家已经执行的动作
    private List<Event> events; // 玩家参与的事件

    private ChatClient chatClient;
    private String systemPrompt;
    private String chatId;

    public Player(int id, String role, Camp camp,
                  ChatClient chatClient, String systemPrompt) {
        this.chatId = UUID.randomUUID().toString();

        // 基础信息
        this.id = id;
        this.role = role;
        this.camp = camp;
        this.isAlive = true;
        this.isSpeech = true;
        this.isKilled = false;
        this.isPoison = false;
        this.actions = actions != null ? new ArrayList<>(actions) : new ArrayList<>();
        this.events = events != null ? new ArrayList<>(events) : new ArrayList<>();

        // 环境配置
        this.chatClient = chatClient;
        this.systemPrompt = systemPrompt;
    }

    public void saveMessage(Event event) {
        this.events.add(event);
    }

    public String chat(String message) {
        String myinfo = "我是" + this.getId() + "号，本局游戏身份是：" + this.getRole();
        return this.chatClient.prompt()
                .system(this.getSystemPrompt())
                .user(myinfo + message + """
                        回答的目标和意向的目标检查保持一致，若没有目标targetNum返回null
                        并严格按照{"targetNum":xx, "content":"xxx对话内容"}回答对话，不要返回其他参数，不要返回``` json ```，这会导致JSON序列化失败
                        """)
                .advisors(a -> a.param(RedisMemoryRepository.CONVERSATION_ID, this.chatId)
                        .param("temperature", 1))
                .call()
                .content();
    }

    public void addEvent(Event actionEvent) {
        if (this.events == null) {
            this.events = new ArrayList<>();
        }
        this.events.add(actionEvent);
    }



    @Deprecated
    public static List<Player> getDefaultPlayers(ChatClient chatClient, String systemPrompt) {
        Player villager0 = new Player(1, "平民", Camp.VILLAGER, chatClient, systemPrompt);
        Player villager1 = new Player(2, "平民", Camp.VILLAGER, chatClient, systemPrompt);
        Player villager2 = new Player(3, "平民", Camp.VILLAGER, chatClient, systemPrompt);
        Player prophet = new Player(4, "预言家", Camp.GOOD_GUY, chatClient, systemPrompt);
        Player witch = new Player(5, "女巫", Camp.GOOD_GUY, chatClient, systemPrompt);
        Player hunter = new Player(6, "猎人", Camp.GOOD_GUY, chatClient, systemPrompt);
        Player wolf1 = new Player(7, "狼人", Camp.WEREWOLF, chatClient, systemPrompt);
        Player wolf2 = new Player(8, "狼人", Camp.WEREWOLF, chatClient, systemPrompt);

        return List.of(villager0, villager1, villager2, prophet, witch, hunter, wolf1, wolf2);
    }


}

